package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.RegisteredYzPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 忘记密码activity（已经没有用了）
 */

public class ForgotPasswordPage2 extends Activity {

    private Button next_step_button;
    private RadioButton login_return;
    private TextView captcha;
    private String phone;//忘记密码的手机号
    private String pass;//忘记密码的手机号原密码
    private Button verification_code;
    private RegisteredYzPageBean CodeBean;//获取注册验证码试实体类
    private int value;
    private EditText Message_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forgot_password_page2);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        setData();
        listener();
    }

    private void setData() {
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone").toString();
        pass = intent.getStringExtra("pass").toString();
        captcha.setText(phone);
    }

    private void initView() {
        next_step_button = (Button) findViewById(R.id.next_step_button);
        login_return = (RadioButton) findViewById(R.id.login_return);
        captcha = (TextView) findViewById(R.id.captcha);
        verification_code = (Button) findViewById(R.id.verification_code);
        Message_code = (EditText) findViewById(R.id.Message_code);
    }

    /**
     * 倒计时:
     */
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            verification_code.setEnabled(false);
            verification_code.setBackgroundColor(ContextCompat.getColor(ForgotPasswordPage2.this,R.color.code));
            verification_code.setText((millisUntilFinished / 1000) + "秒后可重发");
        }
        @Override
        public void onFinish() {
            verification_code.setEnabled(true);
            verification_code.setBackgroundColor(ContextCompat.getColor(ForgotPasswordPage2.this,R.color.code2));
            verification_code.setText("获取验证码");
        }
    };

    private void listener() {
        verification_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                String codeUrl = AppUrl.URL + AppUrl.RegistrationCodeURL;
                Map<String,String> map=new HashMap<>();
                map.put("User_Phone", phone);
                map.put("Type", "2");
                XUtils.Get(codeUrl,map,new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(result);
                            String code = jsonObject.optString("code");
                            if(code.equals("4400")){
                                Gson gson = new Gson();
                                CodeBean = gson.fromJson(result, RegisteredYzPageBean.class);
                                value = CodeBean.getData().getValue();
                                Toast.makeText(ForgotPasswordPage2.this,"已发送验证码",Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(ForgotPasswordPage2.this,"发送验证码失败",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        LogUtil.e(ex.getMessage()+"联网请求失败");
                    }
                });
            }
        });
        next_step_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Message_code.getText().toString().equals(""+value)){
                    Intent intent = new Intent();
                    intent.setClass(ForgotPasswordPage2.this, ForgotPasswordPage3.class);
                    intent.putExtra("phone", phone);
                    intent.putExtra("pass", pass);
                    intent.putExtra("identifying",Message_code.getText().toString());
                    startActivity(intent);
                    overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                }else{
                    Toast.makeText(ForgotPasswordPage2.this,"请输入正确验证码",Toast.LENGTH_SHORT).show();
                }
            }
        });
        login_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}